#ifdef ONLINE_JUDGE /*If compiler is UVa online judge*/
    #include<stdio.h>
    #define INPUT stdin
    int findMax(int boxesNum,int *boxes);
    int findMin(int boxesNum,int *boxes);
    int getAvg(int boxesNum, int *boxes);
    int main() {
#else
    #include"header.h"
    #define INPUT fptIn
    int findMax(int boxesNum,int *boxes);
    int findMin(int boxesNum,int *boxes);
    int getAvg(int boxesNum, int *boxes);
    int acm_00591() {
        printf("--ACM 00591 begin here--\n");
        FILE *fptIn = fopen("Resource/acm_00591.txt","r");
        if ( fptIn == NULL )/*Distinguish the fptInIn file is load success.*/
        {
            printf("File acm_00591.txt load fail.\n");
            printf("Program will exit.\n");
            return EXIT_FAILURE;
        }

#endif
    /*---------- Do someting here begin ----------*/
    int setCounter,boxesNum, boxes[50]={1,2,3,4,5,4,3,2,1};
    int i,canRecive,canGive,maxIndex,minIndex,avg,move;
    fscanf(INPUT," %d",&boxesNum);
    setCounter = 1;
    while( boxesNum > 0 ){
        /*  Read a set of datas begin */
        for(i = 0;i< boxesNum ; i = i + 1){
            fscanf(INPUT," %d",&boxes[i]);
            /*printf("%d  ",boxes[i]);*/
        }
        /*  Read a set of datas end  */
        printf("Set #%d\n",setCounter);
        minIndex = findMin(boxesNum,boxes);
        maxIndex = findMax(boxesNum,boxes);
        avg = getAvg(boxesNum,boxes);
        move = 0;
        while( boxes[minIndex] < avg && boxes[maxIndex] > avg){
            canRecive = avg - boxes[minIndex];
            canGive   = boxes[maxIndex] - avg;
            if( canRecive <= canGive ){
                boxes[minIndex] = boxes[minIndex] + canRecive;
                boxes[maxIndex] = boxes[maxIndex] - canRecive;
                move = move + canRecive;
                /*printf("Take %d from %d to %d\n",canRecive,maxIndex,minIndex);*/
            } else {
                boxes[minIndex] = boxes[minIndex] + canGive;
                boxes[maxIndex] = boxes[maxIndex] - canGive;
                move = move + canGive;
                /*printf("Take %d from %d to %d\n",canGive,maxIndex,minIndex);*/
            }
            /*printArr(boxesNum,boxes);*/
            /*printf("Max is %d\nMin is %d\nMove is %d\n",maxIndex,minIndex,move);*/
            minIndex = findMin(boxesNum,boxes);
            maxIndex = findMax(boxesNum,boxes);
        }
        printf("The minimum number of moves is %d.\n",move);
        fscanf(INPUT," %d",&boxesNum);
        /*if(boxesNum != 0)*/
        /*No need to remove the last blank line*/
            printf("\n");
        setCounter = setCounter + 1;
    }
    /*----------  Do someting here end  ----------*/
    #ifndef ONLINE_JUDGE
        printf("--ACM 00591 end here--\n");
        fclose(fptIn);
    #endif
    return 0;
}
int findMax(int boxesNum,int *boxes){
    int i=0,maxIndex=0;
    for(i = 0, maxIndex = 0; i < boxesNum ; i = i + 1 ){
        if( boxes[i] > boxes[maxIndex] )
        {
            maxIndex = i;
        }
    }
    return maxIndex;
}
int findMin(int boxesNum,int *boxes){
    int i=0,minIndex=0;
    for( i = 0, minIndex = 0; i < boxesNum ; i = i + 1 ){
        if( boxes[i] < boxes[minIndex] )
        {
            minIndex = i;
        }
    }
    return minIndex;
}
int getAvg(int boxesNum, int *boxes){
    int i=0,avg=0;
    for( i = 0, avg = 0; i < boxesNum ; i = i + 1 ){
        avg = avg + boxes[i];
    }
    avg = avg / boxesNum;
    return avg;
}
